package com.fjwt.gz.proxy.ctrl.qrcode;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.QrcodeRecordDTO;
import com.fjwt.gz.db.entity.QrcodeRecordEntity;
import com.fjwt.gz.db.vo.QrcodeRecordVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.QrcodeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 生成卡券二维码记录
 */
@Slf4j
@RestController
@RequestMapping("/api/qrcodeRecord")
public class QrcodeRecordController extends CommonCtrl {

    @Autowired
    private QrcodeRecordService qrcodeRecordService;

    /**
     * 新增二维码记录
     */
    @PostMapping("/saveRecord")
    public ApiRes saveRecord() {
        QrcodeRecordEntity qrcodeRecordEntity = getObject(QrcodeRecordEntity.class);
        log.info("生成卡券二维码接口调用：请求地址：/api/qrcodeRecord/saveRecord，请求参数：{}", JSON.toJSONString(qrcodeRecordEntity));

        qrcodeRecordEntity.setCreatedUid(getCurrentUser().getSysUserId());

        return ApiRes.ok(qrcodeRecordService.saveRecord(qrcodeRecordEntity));
    }

    /**
     * 生成二维码、或者查看二维码
     *
     * @return
     */
    @PostMapping("/getUnlimitedQRCode")
    public ApiRes getUnlimitedQRCode() {
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("pc端生成查看二维码接口调用：请求地址：/api/qrcodeRecord/getUnlimitedQRCode，二维码记录id：{}", qrcodeRecordId);
        if (Objects.isNull(qrcodeRecordId)) {
            return ApiRes.customFail("请求参数缺失！");
        }
        String code = qrcodeRecordService.getUnlimitedQRCode(qrcodeRecordId);
        log.info("pc端生成二维码接口调用结果：请求地址：/api/qrcodeRecord/getUnlimitedQRCode，返回结果：{}", code);
        return ApiRes.ok(code);
    }

    /**
     * 获取机构下生成的领券二维码记录
     *
     * @return
     */
    @PostMapping("/getQrcodeRecordList")
    public ApiRes getQrcodeRecordList() {
        Long activityId = getValLong("activityId");
        String activityNo = getValString("activityNo");
        String createdAt = getValString("createdAt");
        String activityName = getValString("activityName");
        log.info("pc端生成二维码历史记录接口调用：请求地址：/api/qrcodeRecord/getQrcodeRecordList，活动id：{}，活动编号：{}，活动名称：{}", activityId, activityNo, activityName);
        IPage<QrcodeRecordVO> list = qrcodeRecordService.getQrcodeRecordList(getIPage(), activityId, activityNo, activityName, createdAt);
        log.info("pc端生成二维码历史记录接口调用结果：请求地址：/api/qrcodeRecord/getQrcodeRecordList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取活动所有领券二维码
     *
     * @return
     */
    @GetMapping("/getQrcodeRecordAllByActivityId")
    public ResponseEntity<StreamingResponseBody> getQrcodeRecordAllByActivityId() {
        Long activityId = getValLong("activityId");
        log.info("pc端获取活动所有领券二维码接口调用：请求地址：/api/qrcodeRecord/getQrcodeRecordAllListByActivityId，活动id：{}", activityId);
        ResponseEntity<StreamingResponseBody> qrcodeToFile = qrcodeRecordService.getQrcodeToFile(activityId);
        log.info("pc端获取活动所有领券二维码接口调用结果：请求地址：/api/qrcodeRecord/getQrcodeRecordAllListByActivityId，返回结果：{}", JSONUtil.toJsonStr(qrcodeToFile));
        return qrcodeToFile;
    }

    /**
     * 获取二维码详情
     *
     * @return
     */
    @PostMapping("/getRecordDetail")
    public ApiRes getRecordDetail() {
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("pc端获取二维码详情接口调用：请求路径：/api/qrcodeRecord/getRecordDetail，二维码记录id：{}", qrcodeRecordId);
        if (Objects.isNull(qrcodeRecordId)) {
            return ApiRes.customFail("请求参数缺失！");
        }
        QrcodeRecordVO vo = qrcodeRecordService.getRecordDetail(qrcodeRecordId);
        QrcodeRecordVO data = new QrcodeRecordVO();
        BeanUtils.copyProperties(vo,data);
        return ApiRes.ok(data);
    }


    /**
     * 获取当前活动下所有领券二维码记录
     *
     * @return
     */
    @GetMapping("/getAllRecordByActivityId")
    public ApiRes getAllRecordByActivityId(Long activityId) {

        log.info("pc端获取当前活动下所有领券二维码记录：请求地址：/api/qrcodeRecord/getAllRecordByActivityId，活动id：{}", activityId);
        List<QrcodeRecordVO> list = qrcodeRecordService.getAllRecordByActivityId(activityId);
        List<QrcodeRecordVO> vos = List.copyOf(list);
        log.info("pc端获取当前活动下所有领券二维码记录：请求地址：/api/qrcodeRecord/getAllRecordByActivityId，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.ok(vos);
    }


    /**
     * 批量新增生成卡券二维码
     *
     * @return
     */
    @PostMapping("/batchCreateQrCode")
    public ApiRes batchCreateQrCode() {
        QrcodeRecordDTO qrcodeRecordDTO = getObject(QrcodeRecordDTO.class);

        log.info("pc端新增活动渠道码，请求地址：/api/qrcodeRecord/batchCreateQrCode，请求参数：{}", qrcodeRecordDTO);

        // 校验参数
        if (ObjectUtils.isEmpty(qrcodeRecordDTO.getActivityId()) || ObjectUtils.isEmpty(qrcodeRecordDTO.getOssFileUrl())) {
            return ApiRes.customFail("请求参数异常，请检查！");
        }

        qrcodeRecordDTO.setCreateId(getCurrentUser().getSysUserId());
        JSONObject object = qrcodeRecordService.batchCreateQrCode(qrcodeRecordDTO, OssFileConfig.BIZ_TYPE.ACTIVITY);
        log.info("pc端新增活动渠道码，请求地址：/api/qrcodeRecord/batchCreateQrCode，返回结果：{}", JSONUtil.toJsonStr(object));
        return ApiRes.ok(object);
    }


    /**
     * 删除卡券二维码
     *
     * @return
     */
    @DeleteMapping("/deleteQrCode/{qrcodeRecordId}")
    public ApiRes deleteQrCode(@PathVariable Long qrcodeRecordId) {

        log.info("删除二维码接口调用：请求地址：/api/qrcodeRecord/deleteQrCode，二维码记录id：{}", qrcodeRecordId);
        if (Objects.isNull(qrcodeRecordId)) {
            return ApiRes.customFail("请求参数缺失！");
        }
        qrcodeRecordService.removeById(qrcodeRecordId);
        return ApiRes.ok();
    }

}
